/*!

@author
*/
#include <stdafx.h>

#include "SystemEvents.h"



bool SystemEvents::g_running = true;
bool SystemEvents::g_deviceChange = false;

bool SystemEvents::Update()
{
	g_deviceChange = false;
	// Update input: dispatching of system events is tightly related to input update
	gInputManager->Update();

	MSG msg;
	while ( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
	{
		TranslateMessage( &msg );
		DispatchMessage( &msg );
	}
	return g_running;

}

bool SystemEvents::HaveDeviceChange()
{
	return g_deviceChange;
}

LRESULT CALLBACK SystemEvents::WinEventHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_CLOSE:
	case WM_DESTROY:
		g_running = false;
		break;
	case WM_DEVICECHANGE:
		g_deviceChange = true;
		break;
	}

	if (!gInputManager->WinEventHandler(hWnd,msg,wParam,lParam))
		return 0;
	return DefWindowProc(hWnd,msg,wParam,lParam);
}

/*!

*/
bool DirectoryFilter::Initialize(const string& strPath, const string& strMask, bool bFiles, bool bDirectories)
{
	WIN32_FIND_DATAA findData;
	string strFind = strPath + strMask;

	HANDLE hFile = FindFirstFileA(strFind.c_str(), &findData);
	if(hFile == INVALID_HANDLE_VALUE)
		return false;
	BOOL bFoundOne = TRUE;
	while(bFoundOne)
	{
		bool bIsDir = !!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
		bool bAdd	= false;

		if(bIsDir) 
		{
			bAdd = bDirectories;
			if(bDirectories)
			{
				if(findData.cFileName[0] == '.')
					bAdd = false;
			}
		}
		else
			bAdd = bFiles;

		if(bAdd)
			m_lstFiles.push_back(findData.cFileName);
		bFoundOne = FindNextFileA(hFile, &findData);
	}
	return true;
}